﻿
@{
    ViewData["Title"] = "EliminationManagementEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div style="margin-left: 20px; margin-top: 20px;">
    <button id="saveBtn" class="layui-btn" style="background-color:red">
        <i class="layui-icon layui-icon-addition"></i>保存
    </button>
    <br />
    <div class="layui-inline" style="margin-top: 20px;">
        <form id="demo" class="layui-form" action="">
            <div class="layui-form-item" style="margin-left:-32px">
                <div class="layui-inline">
                    <label class="layui-form-label">
                        <span style="color:red">*</span>淘汰日期
                    </label>
                    <div class="layui-input-inline layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-date"></i>
                        </div>
                        <input type="text" name="eliminationDate" id="eliminationDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">羊只类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="sheepType" value="种母" title="种母" checked disabled>
                        <input type="radio" name="sheepType" value="种公" title="种公" disabled>
                        <input type="radio" name="sheepType" value="后裔" title="后裔" disabled>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">
                        单据号
                        <span style="margin-left: 10px;">-- --</span>
                    </label>
                </div>
                <div class="layui-inline">
                    <input type="text" name="id" id="id" style="display:none;">
                    <input type="text" name="reproductiveState" id="reproductiveState" style="display:none;">
                    <input type="text" name="sheepBir" id="sheepBir" style="display:none;">
                    <input type="text" name="eliminationDayAge" id="eliminationDayAge" style="display:none;">
                    <input type="text" name="eliminationMonthAge" id="eliminationMonthAge" style="display:none;">
                    <input type="text" name="documentNumber" id="documentNumber" style="display:none;">
                    <input type="text" name="examineBy" id="examineBy" style="display:none;">
                    <input type="text" name="examineDate" id="examineDate" style="display:none;">
                    <input type="text" name="createBy" id="createBy" style="display:none;">
                    <input type="text" name="createDate" id="createDate" style="display:none;">
                    <input type="text" name="updateBy" id="updateBy" style="display:none;">
                    <input type="text" name="updateDate" id="updateDate" style="display:none;">
                    <input type="text" name="isDelete" id="isDelete" style="display:none;">
                </div>
            </div>
        </form>
    </div>
    <table id="dataTable" class="layui-table" style="width: 98%;">
        <thead>
            <tr>
                <th style="width:120px;text-align: center;">
                    <span style="color:red">*</span>羊只耳号
                </th>
                <th style="width:120px;text-align: center;">品种</th>
                <th style="width:120px;text-align: center;">当前栋舍</th>
                <th style="width:120px;text-align: center;">当前栏位</th>
                <th style="width:120px;text-align: center;">
                    <span style="color:red">*</span>离场状态
                </th>
                <th style="width:120px;text-align: center;">
                    <span style="color:red">*</span>淘汰原因
                </th>
                <th style="width:120px;text-align: center;">
                    <span style="color:red">*</span>处理措施
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td style="text-align: center;">
                    <div class="select-wrapper">
                        <select name="earNumber" id="earNumber" lay-filter="earNumber" lay-verify="required" disabled>
                            <option value="">耳号</option>
                        </select>
                    </div>
                </td>
                
                <td style="text-align: center;">
                    <div class="select-wrapper">
                        <select name="sheepBreed" id="sheepBreed" lay-filter="column" lay-verify="required" disabled>
                            <option value="">品种</option>
                        </select>
                    </div>
                </td>
                <td><input type="text" name="buildings" id="buildings" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" disabled></td>
                <td><input type="text" name="columns" id="columns" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" disabled></td>
                <td style="text-align: center;">
                    <div class="select-wrapper">
                        <select name="isLeave" id="isLeave" lay-filter="build" lay-verify="required">
                            <option value="">离场状态</option>
                            <option value="已离场">已离场</option>
                            <option value="在场">在场</option>
                        </select>
                    </div>
                </td>
                <td style="text-align: center;">
                    <div class="select-wrapper">
                        <select name="eliminationCause" id="eliminationCause" lay-filter="build" lay-verify="required">
                            <option value="">淘汰原因</option>
                        </select>
                    </div>
                </td>
                <td style="text-align: center;">
                    <div class="select-wrapper">
                        <select name="eliminationMeasures" id="eliminationMeasures" lay-filter="build" lay-verify="required">
                            <option value="">处理措施</option>
                            <option value="销售">销售</option>
                            <option value="屠宰">屠宰</option>
                            <option value="无害化处理">无害化处理</option>
                        </select>
                    </div>
                </td>
            </tr>
            <!-- Rows will be dynamically added here -->
        </tbody>
    </table>
</div>
<style>
    /* 为日期输入框添加样式 */
    input[type="date"] {
        padding: 5px;
        border: 1px solid #ccc;
        border-radius: 4px;
        background-color: #f9f9f9;
        font-size: 14px;
        color: #333;
        width: 150px;
        /* 设置一个合适的宽度 */
    }
        /* 您可以根据需要添加其他样式 */
        input[type="date"]:focus {
            border-color: #007BFF;
            outline: none;
        }

    #dataTable {
        width: 100%; /* Ensure table takes full width of its container */
        max-width: 100%; /* Restrict table width to 100% of parent container */
        overflow-x: auto; /* Enable horizontal scrolling */
    }
        /* Ensure table header cells match width of table cells in body */
        #dataTable thead th {
            white-space: nowrap; /* Prevent wrapping of header text */
        }

    /* 模拟layui的下拉框样式 */
    .select-wrapper {
        position: relative;
        display: inline-block;
        font-family: Arial, sans-serif;
        overflow: hidden;
        border: 1px solid #e6e6e6; /* 边框样式 */
        border-radius: 4px; /* 圆角 */
        background-color: #fff; /* 背景颜色 */
    }

        .select-wrapper select {
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            border: none;
            padding: 8px 10px;
            font-size: 14px;
            cursor: pointer;
            width: 200px;
            background: transparent; /* 透明背景 */
        }

        .select-wrapper::after {
            content: '\25BE'; /* 下三角形符号 */
            position: absolute;
            top: 50%;
            right: 10px;
            transform: translateY(-50%);
            pointer-events: none;
        }

        /* 根据选中状态改变样式 */
        .select-wrapper select:focus + ::after {
            color: #009688; /* 选中时的颜色 */
        }
</style>

<script>
    var $ = layui.$;

    layui.use(['table', 'form', 'jquery'], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;

        // 日期
        laydate.render({
            elem: '#eliminationDate',
            value: new Date(),
            isInitValue: true
        });

        var earNumber = window.location.search.split("=")[1];
        //绑定耳号
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesAllInfo',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    var select = $('#earNumber');
                    $.each(res, function (index, item) {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.earNnumber).text(item.earNnumber);
                        select.append(option);
                    });
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });

        // 绑定品种
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/ArchivesGetRout/api/Archives/GetSheepBreeds', // 后端枚举数据接口地址
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    $.each(res, function (index, item) {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.value).text(item.value);
                        $('#sheepBreed').append(option);
                    });
                    // 手动触发 layui 组件的渲染
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });

        //绑定死亡原因
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/SheepManagementGetRout/api/SheepManagement/GetEliminationCause',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    $.each(res, function (index, item) {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.value).text(item.value);
                        $('#eliminationCause').append(option);
                    });
                    // 手动触发 layui 组件的渲染
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });
       
        // 详情
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/SheepManagementGetRout/api/SheepManagement/GetEliminationById?EarNumber=' + earNumber,
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    console.log(res);
                    $("#id").val(res.id);
                    $("#eliminationDate").val(res.eliminationDate.substring(0, 10));
                    $("#eliminationDayAge").val(res.eliminationDayAge);
                    $("#eliminationMonthAge").val(res.eliminationMonthAge);
                    $("#documentNumber").val(res.documentNumber);
                    $("#isLeave").val(res.isLeave);
                    $("#eliminationMeasures").val(res.eliminationMeasures);
                    $("#examineBy").val(res.examineBy);
                    $("#examineDate").val(res.examineDate);
                    $("#createBy").val(res.createBy);
                    $("#createDate").val(res.createDate);
                    $("#updateBy").val(res.updateBy);
                    $("#updateDate").val(res.updateDate);
                    $("#isDelete").val(res.isDelete);
                    setTimeout(function () {
                        $("#earNumber").val(res.earNumber);
                        $("#eliminationCause").val(res.eliminationCause);
                    }, 1);
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });

        // 详情
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesGetByEarNumberInfo?EarNumber=' + earNumber,
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    $("#sheepGender").val(res.sheepGender.toString());
                    $('.layui-input-block input[type="radio"]').each(function () {
                        if ($(this).val() === res.sheepType) {
                            $(this).prop('checked', true);
                        }
                    });
                    layui.form.render('radio');
                    $("#sheepBreed").val(res.sheepBreed);
                    $("#buildings").val(res.cottageId);
                    $("#columns").val(res.field);
                    $("#reproductiveState").val(res.reproductiveState);
                    $("#sheepBir").val(res.sheepBir);
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });
    });

    // 提交羊只淘汰信息
    $('#saveBtn').on('click', function () {

        var formData = {
            id: $("#id").val(),
            earNumber: $('#earNumber').val(),
            eliminationCause: $("#eliminationCause").val(),
            sheepBreed: $("#sheepBreed").val(),
            cottageId: $("#buildings").val(),
            field: $("#columns").val(),
            sheepType: $("[name='sheepType']:checked").val(),
            eliminationDate: $("#eliminationDate").val(),
            reproductiveState: $("#reproductiveState").val(),
            sheepBir: $("#sheepBir").val(),
            sheepGender: $("#sheepGender").val()=="true",
            documentNumber:$("#documentNumber").val(),
            eliminationDayAge: parseInt($("#eliminationDayAge").val()),
            eliminationMonthAge: parseInt($("#eliminationMonthAge").val()),
            isLeave:$("#isLeave").val(),
            eliminationMeasures: $("#eliminationMeasures").val(),
            examineBy: $("#examineBy").val() == "" ? null : $("#examineBy").val(),
            examineDate: $("#examineDate").val() == "" ? null : $("#examineDate").val(),
            createBy: $("#createBy").val() == "" ? null : $("#createBy").val(),
            createDate: $("#createDate").val() == "" ? null : $("#createDate").val(),
            updateBy: $("#updateBy").val() == "" ? null : $("#updateBy").val(),
            updateDate: $("#updateDate").val() == "" ? null : $("#updateDate").val(),
            isDelete: $("#isDelete").val() == "true",
        }
        console.log(formData)

        if (formData.eliminationCause == "") {
            layer.msg("请选择淘汰原因");
            return;
        }

        if (formData.isLeave == "") {
            layer.msg("请选择离场状态");
            return;
        }

        if (formData.eliminationMeasures == "") {
            layer.msg("请选择处理措施");
            return;
        }

        // 修改羊只档案在场状态数据
        var data = {
            EarNumber: formData.earNumber,
            PresenceStatus: "淘汰已离场"
        };

        $.ajax({
            type: 'put',
            //url: 'http://10.31.60.18:8000//BeginningSetRout/api/Beginning/BeginninCreate',
            url: 'http://10.31.60.18:8000/SheepManagementRout/api/SheepManagement/UpdateEliminationManagement',
            data: JSON.stringify(formData), // 将表单数据转为JSON格式
            contentType: 'application/json',
            success: function (response) {
                if (response == 1) {
                    layer.msg('修改成功', function () {
                        if (formData.isLeave == "已离场") {
                            // 更新羊只档案在场状态
                            updateStatus(data);
                        } else {
                            window.location.href = '/SheepManagement/EliminationManagement';
                        }
                    });
                } else {
                    layer.msg('修改失败', { icon: 2 });
                }

            },
            error: function (xhr, status, error) {
                console.error(error);
            }
        });

        // 修改档案羊只在场状态
        function updateStatus(data) {
            $.ajax({
                url: `http://10.31.60.18:8000/ArchivesSetRout/api/Archives/ArchivesUpdateStatus`,
                type: 'put',
                data: JSON.stringify(data),
                contentType: 'application/json',
                success: function (res) {
                    window.location.href = '/SheepManagement/EliminationManagement';
                },
                error: function () {
                    layer.msg('请求失败');
                }
            });
        }

    });
</script>







